package com.amt.basic.service.impl;

import com.amt.basic.config.ResultMsg;
import com.amt.basic.service.GoodsService;
import com.amt.util.HttpUtil;
import com.amt.util.JsonUtil;
import com.amt.util.WeiMengApiUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
@Slf4j
public class GoodsServiceImpl implements GoodsService {
    @Override
    public String addGoods(String param) {
        //默认参数为需要的json数据
        String result = HttpUtil.requestPost(WeiMengApiUtil.GOODS_ADD, param);
        log.info("添加商品响应数据{}",result);
        return result;
    }

    @Override
    public String deleteGoods(String param) {
        return null;
    }

    @Override
    public String priceUpdate(String json) {
        ResultMsg resultMsg = new ResultMsg();
        log.info("商品价格变更订阅消息{}",json);
        Map map = JsonUtil.jsonToHashMap(json);
        HashMap<String,Object> body = (HashMap<String, Object>) map.get("msg_body");
        Long goodsId = (Long) body.get("goodsIdList");
        Long storeId = (Long) body.get("storeId");
        //拿到消息  变更的商品
        if(goodsId!=null && storeId!=null){
            //拿到变更的商品信息 json数据
            String goodInfoById = getGoodInfoById(goodsId);
            resultMsg.setErrcode("0");
            resultMsg.setErrmsg("success");
            return JsonUtil.objectToJson(resultMsg);
        }
        resultMsg.setErrcode("0");
        resultMsg.setErrmsg("error");
        return JsonUtil.objectToJson(resultMsg);
    }

    @Override
    public String goodsUpdate(String json) {
        ResultMsg resultMsg = new ResultMsg();
        log.info("商品信息变更订阅消息{}",json);
        Map map = JsonUtil.jsonToHashMap(json);
        HashMap<String,Object> body = (HashMap<String, Object>) map.get("msg_body");
        Long goodsId = (Long) body.get("goodsIdList");
        //拿到消息  变更的商品
        if(goodsId!=null ){
            //拿到变更的商品信息 json数据
            String goodInfoById = getGoodInfoById(goodsId);



            resultMsg.setErrcode("0");
            resultMsg.setErrmsg("success");
            return JsonUtil.objectToJson(resultMsg);
        }
        resultMsg.setErrcode("0");
        resultMsg.setErrmsg("error");
        return JsonUtil.objectToJson(resultMsg);
    }

    @Override
    public String goodsDelete(String json) {
        ResultMsg resultMsg = new ResultMsg();
        log.info("商品删除变更订阅消息{}",json);
        Map map = JsonUtil.jsonToHashMap(json);
        HashMap<String,Object> body = (HashMap<String, Object>) map.get("msg_body");
        Long goodsId = (Long) body.get("goodsIdList");
        //拿到消息  变更的商品
        if(goodsId!=null ){
            resultMsg.setErrcode("0");
            resultMsg.setErrmsg("success");
            return JsonUtil.objectToJson(resultMsg);
        }
        resultMsg.setErrcode("0");
        resultMsg.setErrmsg("error");
        return JsonUtil.objectToJson(resultMsg);
    }

    /**
     * 根据goodsId获取商品详情
     * @param id
     * @return
     */
    public String getGoodInfoById(Long id){
        HashMap<String, Long> hashMap = new HashMap<>();
        hashMap.put("goodsId",id);
        String result = HttpUtil.requestPost(WeiMengApiUtil.GOODS_INFO_QUERY, JsonUtil.objectToJson(hashMap));
        return result;
    }

}
